package com.stylefeng.guns.wx.modular.scheduled.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.wx.common.persistence.dao.UserNovelMapper;
import com.stylefeng.guns.wx.common.persistence.model.UserNovel;
import com.stylefeng.guns.wx.modular.auth.util.mail.Mail;
import com.stylefeng.guns.wx.modular.auth.util.mail.MailUtil;
import com.stylefeng.guns.wx.modular.scheduled.service.IUserNovelService;
import com.stylefeng.guns.wx.modular.scheduled.util.webmagic.WebmagicQiDianUtil;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserNovelServiceImpl implements IUserNovelService {

    @Resource
    UserNovelMapper userNovelMapper;
    @Resource
    MailUtil mailUtil;
    @Resource
    WebmagicQiDianPipeline pipeline;
    @Override
    public Integer save(UserNovel userNovel) {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_open_id", userNovel.getUserOpenId())
                .eq("novel_id",userNovel.getNovelId());
        List<UserNovel> userNovelList = userNovelMapper.selectList(wrapper);
        if(null==userNovelList||userNovelList.size()==0){
            return 0;//已存在请勿重复添加
        }
        return userNovelMapper.insert(userNovel);
    }

    @Override
    public Integer delete(UserNovel userNovel) {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_open_id", userNovel.getUserOpenId())
                        .eq("novel_id",userNovel.getNovelId());
        return userNovelMapper.delete(wrapper);
    }

    @Override
    public void webmagic() {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("delete_flag", "00");
        List<UserNovel> userNovelList = userNovelMapper.selectList(wrapper);
        Spider spider = Spider.create(new WebmagicQiDianUtil());
        for (UserNovel userNovel : userNovelList) {
            StringBuffer sb = new StringBuffer();
            sb.append("https://book.qidian.com/info/")
                    .append(userNovel.getNovelId());
            spider.addUrl(sb.toString());
        }
        spider.addPipeline(pipeline);
        spider.thread(3).run();
    }

    @Override
    public void reminder() {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("delete_flag", "00")
                        .eq("send_flag","0");
        List<UserNovel> userNovelList = userNovelMapper.selectList(wrapper);
        for (UserNovel userNovel : userNovelList) {
            Mail mail = new Mail();
            mail.setMsg(userNovel.getNovelLatestChapter());
            mail.setSubject(userNovel.getNovelTitle());
            mail.setToEmail(userNovel.getEmail());
            try {
                mailUtil.sendEmail(mail);
                UserNovel u = new UserNovel();
                u.setSendFlag("1");
                u.setLastSendTime(new Date());
                Wrapper<UserNovel> wrapper2 = new EntityWrapper<>();
                wrapper2 = wrapper2.eq("id", userNovel.getId());
                userNovelMapper.update(u,wrapper2);
            }catch (Exception e){
                System.out.println("发送邮件失败:"+e);
                //不处理
            }finally {

            }
        }
    }
}
